/**
 * 
 */
package com.eagle.coders.core.action.meta.processor;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import com.eagle.coders.core.action.meta.processor.vo.UsecaseMetaProcessBean;

/**
 * @author anees
 *
 */
public class UsecaseMetaOutActionProcessor {

	private SortedMap<Integer, String> tablePropertiesForColsMap = new TreeMap<Integer, String>();
	
	private List<UsecaseMetaProcessBean> usecaseMetaProcessBeans;
	
	
	/**
	 * 
	 * @param outURLs
	 */
	public UsecaseMetaOutActionProcessor(final Enumeration<URL> outURLs){
		this.usecaseMetaProcessBeans = processUsecaseOut(outURLs);
	}
	
	
	/**
	 * 
	 * @return
	 */
	public List<UsecaseMetaProcessBean> getUsecaseMetaProcessBeans(){
		return this.usecaseMetaProcessBeans;
	}

	/**
	 * @return the usecaseMetaProcessBeans
	 */
	public List<UsecaseMetaProcessBean> getUsecaseMetaProcessBeans(Enumeration<URL> outURLs) {
		if(null == usecaseMetaProcessBeans)
			return processUsecaseOut(outURLs);
		else
			return usecaseMetaProcessBeans;
	}
	
	/**
	 * 
	 * @return
	 */
	public SortedMap<Integer, String> getTablePropertiesForColsMap(){
		return this.tablePropertiesForColsMap;
	}

	/**
	 * 
	 * @param outURLs
	 * @return
	 */
	public SortedMap<Integer, String> getTablePropertiesForColsMap(Enumeration<URL> outURLs){
		
		usecaseMetaProcessBeans = processUsecaseOut(outURLs);
		
		return this.tablePropertiesForColsMap;
	}
	
	/**
	 * 
	 * @param outURLs
	 * @return
	 */
	private List<UsecaseMetaProcessBean> processUsecaseOut(Enumeration<URL> outURLs){
		
		List<UsecaseMetaProcessBean> list = new ArrayList<UsecaseMetaProcessBean>();
		
		while(outURLs.hasMoreElements()){
			URL outSearchTableURL = outURLs.nextElement();
			try{
				InputStream is= outSearchTableURL.openStream();
				
				 BufferedReader reader = new BufferedReader(
                         new InputStreamReader(is, "UTF-8")); // NOI18N
				 while(true){
					 String line = reader.readLine();
					 if(null == line){
						 break;
					 }
					 
					 if(!line.startsWith("#") && !line.equals("")){
//						 System.out.println("[UsecaseMetaOutActionProcessor] :: line :: "+line);
						 String[] beanProperties =line.split("=");
						 String beanName = beanProperties[0];
//						 System.out.println("[UsecaseMetaOutActionProcessor] :: beanName :: "+beanName);
						 String[] properties = beanProperties[1].split("::");
						 
						 SortedMap<Integer, String> propertiesPositionMap = new TreeMap<Integer, String>();
						 
						 for(String property : properties){
							 String[] propertyWidPosition = property.split("_");
							 
//							 System.out.println("[UsecaseMetaOutActionProcessor] :: propertyWidPosition :: "+propertyWidPosition.length);
							 String domainProperty =propertyWidPosition[0];
							 Integer domainPropertyPositions = Integer.valueOf(propertyWidPosition[1]);
							 propertiesPositionMap.put(domainPropertyPositions, domainProperty);
							 
							 tablePropertiesForColsMap.put(domainPropertyPositions, domainProperty);
						 }

						 UsecaseMetaProcessBean usecaseMetaProcessBean = new UsecaseMetaProcessBean();
						 usecaseMetaProcessBean.setBeanName(beanName);
						 usecaseMetaProcessBean.setPropertyPositionMap(propertiesPositionMap);
						 
						 list.add(usecaseMetaProcessBean);
					 }
				 }
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		return list;
	}
}
